package com.jiang.android.pbutton;

import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * Created by jiang on 2017/3/1.
 */

public class CProgressDrawable extends ShapeElement {

    private static final String TAG = "CircularProgressDrawabl";
    private float mSweepAngle;
    private float mStartAngle;
    private int mSize;
    private int mStrokeWidth;
    private int mStrokeWidthOut;
    private int mStrokeColor;
    private Context mContext;
    private RectFloat mMiddleRect;

    public CProgressDrawable(Context context, int size, int strokeWidth, int stokenWidthOut, int strokeColor) {
        mSize = size;
        this.mContext = context;
        this.mStrokeWidthOut = stokenWidthOut;
        mStrokeWidth = strokeWidth;
        mStrokeColor = strokeColor;
        mStartAngle = -90;
        mSweepAngle = 0;
    }

    public void setSweepAngle(float sweepAngle) {
        mSweepAngle = sweepAngle;
    }

    public int getSize() {
        return mSize;
    }

    @Override
    public void drawToCanvas(Canvas canvas) {
        final Rect bounds = getBounds();

        if (mPath == null) {
            mPath = new Path();
        }


        mPath.reset();
        mPath.addArc(getRect(mStrokeWidthOut), 0, 360);
        mPath.offset(bounds.left, bounds.top);
        canvas.drawPath(mPath, createPaint(mStrokeWidthOut));

        mPath.reset();
        mPath.addArc(getRect(mStrokeWidth), mStartAngle, mSweepAngle);
        mPath.offset(bounds.left, bounds.top);
        canvas.drawPath(mPath, createPaint(mStrokeWidth));

        mPath.reset();
        mPath.addRect(getRectInMiddle(), Path.Direction.COUNTER_CLOCK_WISE);
        mPath.offset(bounds.left, bounds.top);
        canvas.drawPath(mPath, createPaint(0));

    }

    private RectFloat getRectInMiddle() {
        int size = getSize();
        mMiddleRect = new RectFloat(size/3, size/3, size - size/3, size - size/3);
        return mMiddleRect;

    }

    @Override
    public void setAlpha(int alpha) {

    }



    private RectFloat mRectF;
    private Paint mPaint;
    private Path mPath;

    private RectFloat getRect(int stoken) {
        int size = getSize();
        int index = stoken / 2;
        mRectF = new RectFloat(index, index, size - index, size - index);
        return mRectF;
    }

    private Paint createPaint(int stokenWidth) {
        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setColor(new Color(mStrokeColor));
        }
        if(stokenWidth == 0){
            mPaint.setStyle(Paint.Style.FILL_STYLE);
        }else{
            mPaint.setStyle(Paint.Style.STROKE_STYLE);
            mPaint.setStrokeWidth(stokenWidth);
        }


        return mPaint;
    }


}
